// pages/helpcenter/helpcenter.js
// var WxParse = require('../../../wxParse/wxParse.js');
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    //新的参数和数据
    sharedata:{},
    serviceInfo:{},
    isLoadMore: true,
    page: 1,
    totalPage: 0,
    questionList:[],
    detailsInfo:{},
    imgPath: app.globalData.imgPath,
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    this.init();
    this.shareData();
    this._getData();
    this.questionData();
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {

  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
     //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
     wx.showLoading({
      title: '刷新中...',
    })
    this.setData({
      page:1,
      questionList:[],
      isLoadMore: true,
    })
    this.questionData();
  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage() {
    return {
      path: "/packageB/member/helpcenter/helpcenter",
      title: this.data.sharedata.title,
      imageUrl: this.data.sharedata.icon
    };
  },
  onShareTimeline() {
    return {
      path: "/packageB/member/helpcenter/helpcenter",
      title: this.data.sharedata.title,
      imageUrl: this.data.sharedata.icon
    };
  },
  serviceOn(event) {
    let {value} = event.currentTarget.dataset;
    if(value.type == 0) {
      wx.navigateTo({
        url: value.url,
      })
    } else if (value.type == 2) {
      wx.openCustomerServiceChat({
        extInfo: { 
              url:value.url,
        },
        corpId:value.work_id
      });
    } else {

    }
  },
  init(){
    this.setData({
      page: 1,
      questionList:[],
    });
  },
  complaintTo() {
    wx.navigateTo({
      url: '/mircoApp/serviceCenter/complaint/complaint',
    })
  },
  callPhone(event){
    let {value} = event.currentTarget.dataset;
    wx.makePhoneCall({
      phoneNumber: value,
    });
  },
  showPop(){
    if (this.data.customer_open){
      this.setData({
        show1: true
      });
    }
  },
  closePop(){
    this.setData({
      show1: false
    });
  },
  shareData() {
    let urlStr = app.getNetAddresss("plugin.help-center.frontend.controllers.center.share");
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          let data = res.data;
          this.setData({
            sharedata:data
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      }
    });
  },
  _getData() {
    var that = this;
    let urlStr = app.getNetAddresss("plugin.help-center.frontend.controllers.center.index");
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          let data = res.data;
          this.setData({
            serviceInfo:data
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      }
    });
  },
  showTap(e) {
    let {id} = e.currentTarget.dataset;
    this.questionDetails(id);
  },
  questionDetails(id) {
    let urlStr = app.getNetAddresss("plugin.help-center.frontend.controllers.center.index.centent-details");
    app._getNetWork({
      url: urlStr,
      data:{id:id},
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          let data = res.data;
          this.setData({
            detailsInfo:data,
            showbox:true
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      }
    });
  },
  questionData() {
    let query = {
      page:this.data.page,
    };
    let urlStr = app.getNetAddresss("plugin.help-center.frontend.controllers.center.index.centent-list");
    app._postNetWork({
      url: urlStr,
      data:query,
      success:  (resdata) => {
        let res = resdata.data;
        let data = res.data;
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          this.data.totalPage = data.last_page;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          this.setData({
            questionList: data.data
          })
        } else {
          this.data.page = this.data.page - 1;
          this.data.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //获取更多数据
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.totalPage) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      let query = {
        page:this.data.page,
      };
      let urlStr = app.getNetAddresss("plugin.help-center.frontend.controllers.center.index.centent-list");
      app._postNetWork({
        url: urlStr,
        data: query,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data.data;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              questionList : this.data.questionList.concat(list),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  msgboxclose() {
    this.setData({
      showbox: false,
      msg: ''
    });
  }
});
